package com.emspost.utils;

import cn.hutool.core.util.StrUtil;

import java.io.*;
import java.util.regex.*;

public class CsvDataExtractor {
    public static void main(String[] args) {
        String inputFile = "/Users/yugan/Downloads/risk.csv";
        String outputFile = "/Users/yugan/Downloads/extracted_data.csv";
        
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
            
            // 写入CSV表头
            writer.write("userId,disposalCode,timestamp");
            writer.newLine();
            
            String line;
            while ((line = reader.readLine()) != null) {
                // 使用正则表达式匹配CSV格式（考虑引号内的逗号）
                String[] columns = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
                
                if (columns.length >= 6) {
                    // 提取第3列并获取userId
                    String userId = extractUserId(columns[2]);
                    
                    // 提取第4列并获取disposalCode
                    String disposalCode = extractDisposalCode(columns[3]);
                    
                    // 提取第6列的时间（去掉引号）
                    String timestamp = columns[5].replaceAll("^\"|\"$", "");
                    
                    // 写入提取的数据
                    if (userId != null && disposalCode != null) {
                        writer.write(userId + "," + disposalCode + "," + timestamp);
                        writer.newLine();
                    }
                }
            }
            
            System.out.println("数据提取完成，结果已保存到: " + outputFile);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private static String extractUserId(String jsonColumn) {
        // 使用正则表达式从JSON中提取userId
        Pattern pattern = Pattern.compile("userId:([0-9a-zA-Z]+)");
        String replace = StrUtil.replace(jsonColumn, "\"", "");
        Matcher matcher = pattern.matcher(replace);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }
    
    private static String extractDisposalCode(String jsonColumn) {
        // 使用正则表达式从JSON中提取disposalCode
        Pattern pattern = Pattern.compile("disposalCode:([0-9a-zA-Z]+)");
        String replace = StrUtil.replace(jsonColumn, "\"", "");
        Matcher matcher = pattern.matcher(replace);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }
}